/* GlyphSet.cpp
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Function definitions for the GlyphSet class.
*/

#include "GlyphSet.h"

#include "PixMap.h"



GlyphSet::GlyphSet()
{
}



// The copy consturctor and assignment operator make a deep copy of the surface,
// so that the copy can be set to a different color.
GlyphSet::GlyphSet(const GlyphSet &glyphs)
	: Surface(glyphs.MakeCopy())
{
}



GlyphSet &GlyphSet::operator=(const GlyphSet &glyphs)
{
	(Surface::operator=)(glyphs.MakeCopy());
	return *this;
}



// SetColor:
// Fill the image with the given color, preserving the alpha values.
void GlyphSet::SetColor(unsigned char red, unsigned char green, unsigned char blue)
{
	SetColor(Color(red, green, blue));
}



void GlyphSet::SetColor(Uint32 value)
{
	PixMap pix(*this);

	// At the moment, this function works only with 32-bit images.
	if(!*this || pix.Format().BytesPerPixel != 4)
		return;

	// We will preserve whatever fits into the mask, and replace everything else with the new color.
	Uint32 mask = pix.Format().Amask;
	value &= ~mask;

	// NOTE: On 64-bit systems, the size of "unsigned long" is 8 bytes. Therefore it is essential
	// to use a pointer to a type whose size is guranteed to be exactly 4 bytes. This was previously
	// causing very weird bugs on 64-bit Linux.
	for(int row = 0; row < Height(); ++row)
	{
		Uint32 *it = pix.Begin(row);
		Uint32 *end = pix.End(row);
		
		for( ; it != end; ++it)
			*it = ((*it & mask) | value);
	}
}



// LoadImage:
// Load the image from the given path. This is provided here so that classes that inherit from
// GlyphSet need not use SDL_image or perform these checks directly.
void GlyphSet::LoadImage(const string &path)
{
	(Surface::operator=)(SDL_LoadBMP(path.c_str()));
	if(*this)
	{
		SDL_PixelFormat format = GetFormat();
		(Surface::operator=)(SDL_ConvertSurface(data, &format, 0));
	}

	ConvertBMPToAlpha();
}



// Load an image from compiled-in resource data.
void GlyphSet::LoadData(const string &data, int width, int height)
{
	SDL_PixelFormat format = GetFormat();
	(Surface::operator=)(SDL_CreateRGBSurface(
		SDL_SWSURFACE, width, height, 32, 
		format.Rmask, format.Gmask, format.Bmask, format.Amask));
	
	PixMap pix(*this);
	int shift = pix.Format().Ashift;

	string::const_iterator it = data.begin();

	for(int row = 0; row < height; ++row)
	{
		Uint32 *out = pix.Begin(row);
		Uint32 *end = pix.End(row);
		for( ; out != end; ++out)
		{
			Uint32 value = 16 * *it;
			++it;
			value += *it;
			++it;
			value -= 'a' * (1 + 16);
			*out = (value << shift);
		}
	}
}



// ConvertBMPToAlpha:
// Convert a black and white bitmap into an alpha image.
void GlyphSet::ConvertBMPToAlpha()
{
	// At the moment, this function works only with 32-bit images.
	if(!*this)
		return;

	// We will preserve whatever fits into the mask, and replace everything else with the new color.
	PixMap pix(*this);
	Uint32 mask = pix.Format().Amask;

	// NOTE: On 64-bit systems, the size of "unsigned long" is 8 bytes. Therefore it is essential
	// to use a pointer to a type whose size is guranteed to be exactly 4 bytes. This was previously
	// causing very weird bugs on 64-bit Linux.
	for(int row = 0; row < Height(); ++row)
	{
		Uint32 *it = pix.Begin(row);
		Uint32 *end = pix.End(row);
		
		for( ; it != end; ++it)
			*it = ~((*it << 8) | (*it >> 24)) & mask;
	}
}



SDL_PixelFormat GlyphSet::GetFormat()
{
	// Get the format of the display. We may not have created a window yet, so we can't just
	// call SDL_DisplayFormatAlpha. Besides, the display may not have the right bit depth.
	SDL_PixelFormat format = *(SDL_GetVideoInfo()->vfmt);
	if(format.BitsPerPixel == 32)
	{
		// If the display is 32-bit color, convert the image to its pixel format, 
		// but with an alpha channel as well.
		format.Amask = ~(format.Rmask + format.Bmask + format.Gmask);
		format.Aloss = 0;
		format.Ashift = 8 * ((format.Amask > 0xFF) + (format.Amask > 0xFF00) + (format.Amask > 0xFF0000));
	}
	else
	{
		// The display is not 32-bit. For example, we may be running on an OLPC XO laptop
		// with only 16-bit color display. But for correct rendering of the text, we still
		// need to store it as 32-bit RGBA color. So, just use a standard RGBA format:
		format.Rshift = 0;
		format.Rmask = 0xFF << format.Rshift;
		format.Rloss = 0;

		format.Gshift = 8;
		format.Gmask = 0xFF << format.Gshift;
		format.Gloss = 0;

		format.Bshift = 16;
		format.Bmask = 0xFF << format.Bshift;
		format.Bloss = 0;

		format.Ashift = 24;
		format.Amask = 0xFF << format.Ashift;
		format.Aloss = 0;
		
		format.BitsPerPixel = 32;
		format.BytesPerPixel = 4;
	}
	
	return format;
}



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
